<?php

//-----------------------------------
//----------------Current Context----
//-----------------------------------



function saveData($db, $data){
	$db->beginTransaction();
	//$db->exec("BEGIN EXCLUSIVE TRANSACTION");
	date_default_timezone_set('Europe/Paris');
	$mysqltime = date ("Y-m-d H:i:s");
	$sql_saveData="INSERT INTO ex3_evaluation";
	$sql_saveData.=" (email, time, lang, idSAValues, runID, agentGoal, sToPrice, sToSchedule, sToDayOff, sToAverage, uToPrice, uToSchedule, uToDayOff, uToAverage, ";
	$sql_saveData.=" csCheap, csAverage, csExpensive, csMorning, csAfternoon, csNight, csSunday, csMonday, csTuesday, csWednesday, csThursday, csFriday, csSaturday, ";
	$sql_saveData.=" cuCheap, cuAverage, cuExpensive, cuMorning, cuAfternoon, cuNight, cuSunday, cuMonday, cuTuesday, cuWednesday, cuThursday, cuFriday, cuSaturday, ";
	$sql_saveData.=" asCheap, asAverage, asExpensive, asMorning, asAfternoon, asNight, asSunday, asMonday, asTuesday, asWednesday, asThursday, asFriday, asSaturday, ";
	$sql_saveData.=" auCheap, auAverage, auExpensive, auMorning, auAfternoon, auNight, auSunday, auMonday, auTuesday, auWednesday, auThursday, auFriday, auSaturday, ";
	$sql_saveData.=" mCheap, mAverage, mExpensive, mMorning, mAfternoon, mNight, mSunday, mMonday, mTuesday, mWednesday, mThursday, mFriday, mSaturday) ";
	$sql_saveData.=" VALUES('".$data['email']."', '".$mysqltime."', '".$data['lang']."', ".$data['aID'].", ".$data['aRunID'].", '".$data['aAgentGoal']."', ";
	$sql_saveData.= $data['sToPrice'].", "	.$data['sToSchedule'].", "	.$data['sToDayOff'].", "	.$data['sToAverage'].", "	.$data['uToPrice'].", ".$data['uToSchedule'].", ".$data['uToDayOff'].", ".$data['uToAverage'].", " ;
	$sql_saveData.= $data['csCheap'].", "	.$data['csAverage'].", "	.$data['csExpensive'].", "	.$data['csMorning'].", "	.$data['csAfternoon'].", "	.$data['csNight'].", "	.$data['csSunday'].", "	.$data['csMonday'].", "	.$data['csTuesday'].", "	.$data['csWednesday'].", "	.$data['csThursday'].", "	.$data['csFriday'].", "	.$data['csSaturday'].", " ;
	$sql_saveData.= $data['cuCheap'].", ".	$data['cuAverage'].", "		.$data['cuExpensive'].", "	.$data['cuMorning'].", "	.$data['cuAfternoon'].", "	.$data['cuNight'].", "	.$data['cuSunday'].", "	.$data['cuMonday'].", "	.$data['cuTuesday'].", "	.$data['cuWednesday'].", "	.$data['cuThursday'].", "	.$data['cuFriday'].", "	.$data['cuSaturday'].", " ;
	$sql_saveData.= $data['asCheap'].", "	.$data['asAverage'].", "	.$data['asExpensive'].", "	.$data['asMorning'].", "	.$data['asAfternoon'].", "	.$data['asNight'].", "	.$data['asSunday'].", "	.$data['asMonday'].", "	.$data['asTuesday'].", "	.$data['asWednesday'].", "	.$data['asThursday'].", "	.$data['asFriday'].", "	.$data['asSaturday'].", " ;
	$sql_saveData.= $data['auCheap'].", "	.$data['auAverage'].", "	.$data['auExpensive'].", "	.$data['auMorning'].", "	.$data['auAfternoon'].", "	.$data['auNight'].", "	.$data['auSunday'].", "	.$data['auMonday'].", "	.$data['auTuesday'].", "	.$data['auWednesday'].", "	.$data['auThursday'].", "	.$data['auFriday'].", "	.$data['auSaturday'].", " ;
	$sql_saveData.= $data['mCheap'].", "	.$data['mAverage'].", "		.$data['mExpensive'].", "	.$data['mMorning'].", "		.$data['mAfternoon'].", "	.$data['mNight'].", "	.$data['mSunday'].", "	.$data['mMonday'].", "	.$data['mTuesday'].", "		.$data['mWednesday'].", "	.$data['mThursday'].", "	.$data['mFriday'].", "	.$data['mSaturday'];
	$sql_saveData.=" ); ";
	
	//echo $sql_saveData;
	$db->query($sql_saveData);
	/*if($db->query($sql_saveData)){
		echo 'seccsess';
	}else{
		echo 'fail';
	}*/
	/* Commit the changes */
	//$db->commit();
	//$sth = $db->prepare($sql_saveData); 
	//$sth->execute();
	$db->commit();
	
}

function saveData2($db, $data){
	$db->beginTransaction();
	//$db->exec("BEGIN EXCLUSIVE TRANSACTION");
	$mysqltime = date ("Y-m-d H:i:s");
	$sql_saveData="INSERT INTO ex3_evaluation";
	$sql_saveData.=" (email, time, lang, idSAValues, runID, agentGoal, sToPrice, sToSchedule, sToDayOff, sToAverage, uToPrice, uToSchedule, uToDayOff, uToAverage) ";
	$sql_saveData.=" VALUES('".$data['email']."', '".$mysqltime."', '".$data['lang']."', ".$data['aID'].", ".$data['aRunID'].", '".$data['aAgentGoal']."', ";
	$sql_saveData.= $data['sToPrice'].", "	.$data['sToSchedule'].", "	.$data['sToDayOff'].", "	.$data['sToAverage'].", "	.$data['uToPrice'].", ".$data['uToSchedule'].", ".$data['uToDayOff'].", ".$data['uToAverage'];
	$sql_saveData.=" ); ";

	echo $sql_saveData;

	if($db->query($sql_saveData)){
		echo 'seccsess';
	}else{
		echo 'fail';
	}
	/* Commit the changes */
	//$db->commit();
	//$sth = $db->prepare($sql_saveData);
	//$sth->execute();
	$db->commit();

}


function cenas($db, $data){
	$email = $time = '';
	$insert_stmt = $db->prepare("insert into ex3_evaluation (:email, :time) values (name, age)");
	$insert_stmt->bindValue(':email', $name);
	$insert_stmt->bindValue(':time', $age);
}



/******************************************************************************************
 Returns the surprise and uncertainty values that the Agent has, given a specific 'ID'
******************************************************************************************/
function getAgentSAValues($db, $id){
	$sql_getData="SELECT * from ex3_agentX_SAValues where id=".$id." ";
	foreach ($db->query($sql_getData) as $row2) {
		$aID=$row2[0];
		$aPoiID=$row2[1];
		$aRunID=$row2[28];
		$aAgentGoal=$row2[29];

		$asCheap=$row2[2];
		$asAverage=$row2[3];
		$asExpensive=$row2[4];

		$asMorning=$row2[5];
		$asAfternoon=$row2[6];
		$asNight=$row2[7];

		$asSunday=$row2[8];
		$asMonday=$row2[9];
		$asTuesday=$row2[10];
		$asWednesday=$row2[11];
		$asThursday=$row2[12];
		$asFriday=$row2[13];
		$asSaturday=$row2[14];


		$auCheap=$row2[15];
		$auAverage=$row2[16];
		$auExpensive=$row2[17];

		$auMorning=$row2[18];
		$auAfternoon=$row2[19];
		$auNight=$row2[20];

		$auSunday=$row2[21];
		$auMonday=$row2[22];
		$auTuesday=$row2[23];
		$auWednesday=$row2[24];
		$auThursday=$row2[25];
		$auFriday=$row2[26];
		$auSaturday=$row2[27];

		$x=array (
				"aID"=>$aID,
				"aPoiID"=>$aPoiID,
				"aRunID"=>$aRunID,
				"aAgentGoal"=>$aAgentGoal,

				"asCheap"=>$asCheap,
				"asAverage"=>$asAverage,
				"asExpensive"=>$asExpensive,

				"asMorning"=>$asMorning,
				"asAfternoon"=>$asAfternoon,
				"asNight"=>$asNight,

				"asSunday"=>$asSunday,
				"asMonday"=>$asMonday,
				"asTuesday"=>$asTuesday,
				"asWednesday"=>$asWednesday,
				"asThursday"=>$asThursday,
				"asFriday"=>$asFriday,
				"asSaturday"=>$asSaturday,

				"auCheap"=>$auCheap,
				"auAverage"=>$auAverage,
				"auExpensive"=>$auExpensive,

				"auMorning"=>$auMorning,
				"auAfternoon"=>$auAfternoon,
				"auNight"=>$auNight,

				"auSunday"=>$auSunday,
				"auMonday"=>$auMonday,
				"auTuesday"=>$auTuesday,
				"auWednesday"=>$auWednesday,
				"auThursday"=>$auThursday,
				"auFriday"=>$auFriday,
				"auSaturday"=>$auSaturday
		);
		return $x;

	}
}


/******************************************************************************************
 			Returns the last question number ('ID') answered by the user 
******************************************************************************************/
function APAGARgetCurrentAnswer($db, $email){
	$id=0;
	$sql_userExists="SELECT MAX(idSAValues) from ex3_evaluation where email='".$email."' ";
	$nr=0;

	foreach ($db->query($sql_userExists) as $row) {
		$nr++;
		$id=$row["idSAValues"];
	}
	if($id==0)
		$id=getFirstID($db);

	return $id;
}

/******************************************************************************************
 Returns the last question number ('ID') answered by the user
******************************************************************************************/
function getCurrentAnswer($db, $email){
	$sql_userExists="SELECT MAX(idSAValues) from ex3_evaluation where email='".$email."' ";

	foreach ($db->query($sql_userExists) as $row) {
		return $row[0];
	}
}

/******************************************************************************************
 					Returns the number of question answered by the user
******************************************************************************************/
function getAnswersNumber($db, $email){
	$sql_userExists="SELECT COUNT(*) from ex3_evaluation where email='".$email."' ";

	foreach ($db->query($sql_userExists) as $row) {
		return $row[0];
	}

}

/******************************************************************************************
Returns the 'Goal' associated to a specific 'ID' in the table 'ex3_agentX_SAValues'
******************************************************************************************/
function getGoalFromID($db, $id){
	$sql_getGoal="SELECT agentGoal from ex3_agentX_SAValues where id='".$id."' ";
	foreach ($db->query($sql_getGoal) as $row) {
		return $row["agentGoal"];
	}
}


/******************************************************************************************
 		Returns the real knowledge about a specific POI, given a specific 'ID'
******************************************************************************************/
function getMasterKnowledge($db, $id){
	$sql_getData="SELECT price, schedule, daysOff from ex3_masterknowledge where id=".$id." ";
	foreach ($db->query($sql_getData) as $row2) {
		//-----price
		$price = $row2['price'];
		$price = trim($price);
	
		if(strcmp($price, "Cheap")==0){
			$mCheap=100;
			$mAverage=0;
			$mExpensive=0;
		}elseif(strcmp($price, "Average")==0){
			$mCheap=0;
			$mAverage=100;
			$mExpensive=0;
		}else{
			$mCheap=0;
			$mAverage=0;
			$mExpensive=100;
		}
		
		//-----DayOff
		$daysOff = $row2['daysOff'];
		$daysOff=trim($daysOff);
		
		if(strcmp($daysOff, "Never")==0){
			$daysOff = 'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday';
		}
		$tempDaysOff=getDayOff($daysOff);
		
		$mSunday=	$tempDaysOff['Sunday'];
		$mMonday=	$tempDaysOff['Monday'];
		$mTuesday=	$tempDaysOff['Tuesday'];
		$mWednesday=$tempDaysOff['Wednesday'];
		$mThursday=	$tempDaysOff['Thursday'];
		$mFriday=	$tempDaysOff['Friday'];
		$mSaturday=	$tempDaysOff['Saturday'];
		
		
		
		//-----Schedule		
		$schedule = $row2['schedule'];
		$schedule=trim($schedule);
		$tempSchedule=getSchedule($schedule);

		$mMorning=	$tempSchedule['Morning'];
		$mAfternoon=$tempSchedule['Afternoon'];
		$mNight=	$tempSchedule['Night'];



		$x=array (
	
				"mCheap"=>$mCheap,
				"mAverage"=>$mAverage,
				"mExpensive"=>$mExpensive,
		
				"mMorning"=>$mMorning,
				"mAfternoon"=>$mAfternoon,
				"mNight"=>$mNight,
		
				"mSunday"=>$mSunday,
				"mMonday"=>$mMonday,
				"mTuesday"=>$mTuesday,
				"mWednesday"=>$mWednesday,
				"mThursday"=>$mThursday,
				"mFriday"=>$mFriday,
				"mSaturday"=>$mSaturday
		);
		return $x; 

	}
}


/******************************************************************************************
 								private function
******************************************************************************************/
function getDayOff($daysOff){
	$daysOfTheWeek= array ('Sunday'=>0,
			'Monday'=>0,
			'Tuesday'=>0,
			'Wednesday'=>0,
			'Thursday'=>0,
			'Friday'=>0,
			'Saturday'=>0);
	$daysOfTheWeekTemp= array ('Sunday'=>0,
			'Monday'=>0,
			'Tuesday'=>0,
			'Wednesday'=>0,
			'Thursday'=>0,
			'Friday'=>0,
			'Saturday'=>0);
	$dOff = explode(',', $daysOff);
	
	foreach ($dOff as $id => $name){
		if (array_key_exists(trim($name), $daysOfTheWeek)) {
			$daysOfTheWeekTemp[trim($name)]=round((100/sizeOf($dOff)),2);
		}
	}
	
	return $daysOfTheWeekTemp;	
}

/******************************************************************************************
 									private function
******************************************************************************************/
function getSchedule($s){
	$sch= array ('Morning'=>0,
			'Afternoon'=>0,
			'Night'=>0);
	$scheduleTemp= array ('Morning'=>0,
			'Afternoon'=>0,
			'Night'=>0);
	
	$ss = explode(',', $s);
		
	foreach ($ss as $id => $name){
		if (array_key_exists(trim($name), $sch)) {
			$scheduleTemp[trim($name)]=round((100/sizeof($ss)) , 2);
		}
	}
	
	return $scheduleTemp;
}

/******************************************************************************************
 Returns the 'poiID', given a specific ID
******************************************************************************************/
function getPOIID($db, $id){
	$sql="SELECT poiID from ex3_agentX_SAValues where id=".$id." ";
	foreach ($db->query($sql) as $row2) {
		return $row2[0];
	}
}

/******************************************************************************************
 						Returns the 'ID' for the first quention
******************************************************************************************/
function getFirstID($db){
	$sql="SELECT MIN(id) from ex3_agentX_SAValues where runID=101";
	foreach ($db->query($sql) as $row2) {
		return $row2[0];
	}	
}


/******************************************************************************************
 Returns the surprise and uncertainty mode until a specific 'ID', given a specific 'Goal'
******************************************************************************************/
function getUserModel($db, $id, $goal){
	$sql_getData="SELECT
			AVG(s_price_cheap),
			AVG(s_price_average),
			AVG(s_price_expensive),
			AVG(s_schedule_morning),
			AVG(s_schedule_afternoon),
			AVG(s_schedule_night),
			AVG(s_dayoff_sunday),
			AVG(s_dayoff_monday),
			AVG(s_dayoff_tuesday),
			AVG(s_dayoff_wednesday),
			AVG(s_dayoff_thursday),
			AVG(s_dayoff_friday),
			AVG(s_dayoff_saturday),
			AVG(u_price_cheap),
			AVG(u_price_average),
			AVG(u_price_expensive),
			AVG(u_schedule_morning),
			AVG(u_schedule_afternoon),
			AVG(u_schedule_night),
			AVG(u_dayoff_sunday),
			AVG(u_dayoff_monday),
			AVG(u_dayoff_tuesday),
			AVG(u_dayoff_wednesday),
			AVG(u_dayoff_thursday),
			AVG(u_dayoff_friday),
			AVG(u_dayoff_saturday)
			from ex3_agentX_SAValues where id<".$id." AND agentGoal='".$goal."' ";

	foreach ($db->query($sql_getData) as $row2) {
		$csCheap=$row2[0];
		$csAverage=$row2[1];
		$csExpensive=$row2[2];

		$csMorning=$row2[3];
		$csAfternoon=$row2[4];
		$csNight=$row2[5];

		$csSunday=$row2[6];
		$csMonday=$row2[7];
		$csTuesday=$row2[8];
		$csWednesday=$row2[9];
		$csThursday=$row2[10];
		$csFriday=$row2[11];
		$csSaturday=$row2[12];


		$cuCheap=$row2[13];
		$cuAverage=$row2[14];
		$cuExpensive=$row2[15];

		$cuMorning=$row2[16];
		$cuAfternoon=$row2[17];
		$cuNight=$row2[18];

		$cuSunday=$row2[19];
		$cuMonday=$row2[20];
		$cuTuesday=$row2[21];
		$cuWednesday=$row2[22];
		$cuThursday=$row2[23];
		$cuFriday=$row2[24];
		$cuSaturday=$row2[25];

		$x=array (
				"csCheap"=>$csCheap,
				"csAverage"=>$csAverage,
				"csExpensive"=>$csExpensive,

				"csMorning"=>$csMorning,
				"csAfternoon"=>$csAfternoon,
				"csNight"=>$csNight,

				"csSunday"=>$csSunday,
				"csMonday"=>$csMonday,
				"csTuesday"=>$csTuesday,
				"csWednesday"=>$csWednesday,
				"csThursday"=>$csThursday,
				"csFriday"=>$csFriday,
				"csSaturday"=>$csSaturday,

				"cuCheap"=>$cuCheap,
				"cuAverage"=>$cuAverage,
				"cuExpensive"=>$cuExpensive,

				"cuMorning"=>$cuMorning,
				"cuAfternoon"=>$cuAfternoon,
				"cuNight"=>$cuNight,

				"cuSunday"=>$cuSunday,
				"cuMonday"=>$cuMonday,
				"cuTuesday"=>$cuTuesday,
				"cuWednesday"=>$cuWednesday,
				"cuThursday"=>$cuThursday,
				"cuFriday"=>$cuFriday,
				"cuSaturday"=>$cuSaturday
		);

		return $x;
	}

}
?>